<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;

use app\models\CrmNotify_;
use app\models\search\CrmNotifySearch;

class CrmNotifyController extends CommonController
{

    public function actionList()
    {
        $search= new CrmNotifySearch();
        $pro = $search->search(\Yii::$app->request->post());
        $count = $pro->query->count();
        isset($_POST['limit'])?$pro->query->limit($_POST['limit']):"";
        isset($_POST['page'])?$pro->query->offset($_POST['limit']*($_POST['page']-1)):"";
        $pro->query->select('crm_system_ygxx.ygmc,crm_notify.*')->leftJoin('crm_system_ygxx','crm_system_ygxx.user_id=crm_notify.user_id');
        $res = $pro->query->asArray()->all();
        return $this->actionLayData($count,$res,1,"");
    }

    /**
     * 新增数据
     * @return string
     */
    public function actionSave()
    {
        $model = new CrmNotify_();
        $post = \Yii::$app->request->post();
        $model->create_at = ''.time();
        $model->update_at = ''.time();
        $model->user_id = \Yii::$app->user->getId();
        if ($model->load($post) && $model->validate() && $model->save()) {
            return $this->dealFD('新增成功', 1);
        } else {
            return $this->dealFD(array_values($model->getErrors()), 0);
        }
    }
    /**
     * 修改
     * @return string
     */
    public function actionUpdate()
    {
        $model = new CrmNotify_();
        $post = \Yii::$app->request->post();
        $crmNotify = $model->find()->select('*')->where(['id' => $post['CrmNotify_']['id']])->one();
        if (!empty($crmNotify) && $crmNotify->load($post) && $crmNotify->validate() && $crmNotify->update()) {
            return $this->dealFD('修改成功', 1);
        } else {
            return $this->dealFD(array_values($crmNotify->getErrors()), 0);
        }
    }

}